<?php

# CUI/CF valifation function
function validate_cif($cif)
{
        // Cheia de validare: 753217532
        static $validare="753217532";
        static $lungimeValidare=9; // strlen($validare)

        // Verifică dacă e întreg
        if ((int) $cif != $cif)
                return false;

        $cif = (int) $cif;

        if (strlen($cif) > 10)
                return false;

        // Extrage cifra de control
        $cifra_control = substr($cif, -1);
        $cif = substr($cif, 0, -1);

        // Adaugă zerouri în prefix pâna când CIF are 9 cifre
        while (strlen($cif)<9)
                $cif = '0' . $cif;

        // Calculează suma de control;
        // nu mai este nevoie de inversare (avem zerouri în primele poziții)
        $suma = 0;
        for($i=0; $i<$lungimeValidare; $i++)
                $suma += $cif[$i] * $validare[$i];

        $rest = $suma*10 % 11;

        if ($rest == 10)
                $rest=0;

        return $rest == $cifra_control;
 }
 
 # Generate form content based on provided data
function generate_form_content($array_data)
{
	if( !isset($array_data))
	{
		return "";
	}
		
	$get_form_content = "";
	$bFirst = 1;
									
	foreach($array_data as $form_field)
	{
		$change_row = 1;
		
		if(isset($form_field['same_row']))
			if ($form_field['same_row'] == "yes")
					$change_row = 0;
		
		if ( $bFirst )
		{
			$get_form_content .= "<tr>";
		}
		else
		{
			if( $change_row )
			{
				$get_form_content .= "</tr>";	
				$get_form_content .= "<tr>";
			}	
		}			
		
		$bFirst = 0;			
		
		if( isset($form_field['header']))
		{
			$get_form_content .= "</tr><tr><td>".$form_field['header']."</td></tr><tr>";
		}
			
		if( ! isset( $form_field['only_form_element'] ) )
			$get_form_content .= "<td><b><font color=\"".$form_field['current_color']."\">" . $form_field['field_display_name'] . "</font></b></td>";
		
		if( $form_field['field_type'] == 'text' )
		{
			$get_form_content .= "<td><input type =\"text\" name =\"" . $form_field['post_name'] . "\"";
			$get_form_content .= " value=\"" . $form_field['current_value'] . "\"";
			

			$get_form_content .= " size=\"" . $form_field['field_size'] . "\"";
			$get_form_content .= " maxlength=\"" . $form_field['max_length'] . "\"";
			$get_form_content .= " ></td>";
		}
		else if( $form_field['field_type'] == 'drop_down_list' )
		{
			
			if( isset($form_field['initial_values']) )
				$drop_down_list_options = drop_down_list_generator($form_field['initial_values'],$form_field['current_value']);	
			else
				$drop_down_list_options = drop_down_list_generator(null);	
			$get_form_content .= "<td><select name=\"".$form_field['post_name']."\">";
			$get_form_content .= $drop_down_list_options;
			$get_form_content .= "</select></td>";						
		}
		
		
		if( isset( $form_field['extra_space_after'] ))
		{
			$get_form_content .= "</tr>";	
			$get_form_content .= "<tr>";	
			$get_form_content .="<td></td>";
			$get_form_content .= "</tr>";	
			$get_form_content .= "<tr>";	
		}
	}		
	$get_form_content .="</tr>";
	return $get_form_content;
}

# Function used to generate drop down list menu
function drop_down_list_generator($drop_down_list_options_array, $last_selected_value = null)
{ 
	$drop_down_list_options = "";				   
	
	
	if( null == $drop_down_list_options_array )
			return $drop_down_list_options;	
	
	# Default value used when drop-down list is displayed	
	$drop_down_list_options = $drop_down_list_options."<option value=\"neselectat\">...</option>";
					
	# foreach element in dropdown-list render options
	# if last_selected_value is different than null than select that value as being the default one displayed in interface 
	
	
	foreach($drop_down_list_options_array as $drop_down_list_element_value)
	{
		
		if ( $last_selected_value != null )
		{
			if( $last_selected_value == $drop_down_list_element_value )
			{
				# if one of the options in drop-down list matches the provided value for last_selected_value make it the default one
				$drop_down_list_options = $drop_down_list_options."<option selected=\"selected\" value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";	
			}
			else
			{
				$drop_down_list_options = $drop_down_list_options."<option value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";	
			}
			
		}
		else
		{
			$drop_down_list_options = $drop_down_list_options."<option value='$drop_down_list_element_value'>$drop_down_list_element_value</option>";
		}
	}
	return $drop_down_list_options;
}


?>
